<?php
class ActivityConf_createLuaByExcel {
	function createLuaByExcel($excelPath, $activityInfo, $fileName, $sheet = '0') {
		
		$gameEn = $activityInfo['activityNameEn'];
		
		if(strrpos(strtolower(PHP_OS),"win") === false) { 
			// linux
			$txtDir = ROOT_PATH . "/activityFiles/txt/{$gameEn}/";
			$luaDir = ROOT_PATH . "/activityFiles/lua/{$gameEn}/";
		} else {
			// windows 
			$txtDir = ROOT_PATH . "\\activityFiles\\txt\\{$gameEn}\\";
			$luaDir = ROOT_PATH . "\\activityFiles\\lua\\{$gameEn}\\";
		}
		
		$txtFileName  = $activityInfo['xmlLua'][$fileName]['txt'];
		$luaFileName  = $activityInfo['xmlLua'][$fileName]['lua'];
		
		// 生成lua路径
		$luaPath = $luaDir . $luaFileName;
		$txtPath = $txtDir . $txtFileName;
		
		$dataArr  = MooObj::get('Control_ExcelConfMaker_Excel')->read($excelPath, 'UTF-8', $sheet, true);
		$dataArr = MooObj::get('ActivityConf')->formatExcelData($dataArr, "lua");
		
		// 路径不存在则创建目录
		if(!MooFile::isExists($txtDir)) {
			MooFile::mkDir($txtDir);
		}
		
		if(!MooFile::isExists($luaDir)) {
			MooFile::mkDir($luaDir);
		}
		
		// 文件内容为空
		if(!$dataArr) {
			return array(false, 0);
		}
		
		// 拼装 txt 文件
		$txtStr = null;
		foreach ($dataArr as $key => $val) {
			$s = null;
			foreach ($val as $k => $v) {
				if($s == null) {
					$s .=   $v;
				} else {
					$s.= "	" . $v;
				}
			}
			if($txtStr == null) {
				$txtStr .= $s;
			} else {
				$txtStr .=  "\n".$s;
			}
		}
		// 写入txt文件
		$createTxtRes = MooFile::write($txtPath, $txtStr);
		// 创建txt文件失败
		if(!$createTxtRes) {
			return array(false, 0);
		}
		
		// 判断是windows 还是linux
		if(strrpos(strtolower(PHP_OS),"win") === false) {
			// 非windows 认为 linux
			// $txt2lua = MooConfig::get('main.txt2lua');
			$txt2lua = ROOT_PATH . '/tools/txt2lua';
			
			$execCommand = $txt2lua. ' ' . $txtPath . ' ' . $luaDir;
			
			MooFile::write('execCommand.log', $execCommand . "\n", true);
			
			// 创建lua文件
			exec($execCommand, $output, $return_val);
			
			
		} else {
			// windows
			$ConvertTxtToLuaExe = ROOT_PATH . '\\tools\\ConvertTxtToLua.exe TXT '.$txtDir. ' ' . $luaDir . ' txt ' . $txtFileName;
			
			// 创建lua文件
			exec($ConvertTxtToLuaExe, $output, $return_val);
		}
		
		// 生成xml失败
		if($return_val != 0) {
			return array(false, 0);
		}
		
		return array(true, $luaPath);
	}
}